﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Configuration;
using DSPL.Contracts;
using DSPL.Contracts.Interfaces;
using Microsoft.Practices.Unity;

namespace DSPL.Common.CommonControls.Controls
{
   public  class TxtCurRate: TxtNumber
    {
        private object _bindCode;
       

        #region Properties
        public object BindCode
        {
            get { return _bindCode; }
            set { _bindCode = value; }
        }

        public ICommonRules CurRateCommonRules
        {
            get;
            set;
        }
        #endregion


        protected override void OnLostFocus(RoutedEventArgs e)
        {
            base.OnLostFocus(e);

            if (BindCode !=null)
            {
                ResolveDependencies();
                if (CurRateCommonRules != null)
                {
                    double curRate = CurRateCommonRules.ValidateCurrencyRate(((TextBox)BindCode).Text, Convert.ToDouble(this.Text));
                    this.Text = Convert.ToString(curRate);
                    if (curRate == 0)
                    {
                        this.Focus();
                    }
                }
            }
        }

        protected override void ResolveDependencies()
        {
            base.ResolveDependencies();
            if (base.MasterUnityContainer != null)
            {
                IUnityContainer masterUnityContainer = (IUnityContainer)base.MasterUnityContainer;
                if (CurRateCommonRules == null)
                {
                    CurRateCommonRules = masterUnityContainer.Resolve<ICommonRules>();
                }
            }
        }
    }
}
